package com.authine.cloudpivot.ext.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;


public class TaxRateUtils {

    /**
     * 根据含税价 计算 税前金额
     *
     * @param taxAfter 含税价
     * @param rate     税率
     * @return taxBefore 税前价
     */
    public static BigDecimal calculateTaxBefore(BigDecimal taxAfter, BigDecimal rate) {
        return BigDecimalUtils.div(taxAfter, rate.add(BigDecimal.ONE), 11);
    }

    /**
     * 根据税前价 计算 含税价
     *
     * @param taxBefore 税前价
     * @param rate      税率
     * @return taxAfter 含税价
     */
    public static BigDecimal calculateTaxAfter(BigDecimal taxBefore, BigDecimal rate) {
        return taxBefore.multiply(rate.add(BigDecimal.ONE));
    }

    /**
     * 根据税前价 计算 应缴税款
     *
     * @param taxBefore 税前价
     * @param rate      税率
     * @return taxPayable 应缴税款
     */
    public static BigDecimal calculateTaxPayableByTaxBefore(BigDecimal taxBefore, BigDecimal rate) {
        BigDecimal taxAfter = TaxRateUtils.calculateTaxAfter(taxBefore, rate);
        return taxAfter.subtract(taxBefore);
    }

    /**
     * 根据含税价 计算 应缴税款
     *
     * @param taxAfter 含税价
     * @param rate     税率
     * @return taxPayable 应缴税款
     */
    public static BigDecimal calculateTaxPayableByTaxAfter(BigDecimal taxAfter, BigDecimal rate) {
        BigDecimal taxBefore = TaxRateUtils.calculateTaxBefore(taxAfter, rate);//税前金额
        return taxAfter.subtract(taxBefore);
    }


    public static void main(String[] args) {
        BigDecimal num = new BigDecimal(100);
        BigDecimal v1 = TaxRateUtils.calculateTaxBefore(num, new BigDecimal(0.17));
        System.out.println("根据含税价 " + num + " 计算 税前金额:" + v1);
        BigDecimal v2 = TaxRateUtils.calculateTaxAfter(num, new BigDecimal(0.17));
        System.out.println("根据税前价 " + num + " 计算 含税价:" + v2);
        BigDecimal v3 = TaxRateUtils.calculateTaxPayableByTaxAfter(num, new BigDecimal(0.17));
        System.out.println("根据含税价 " + num + " 计算 应缴税款:" + v3);
        BigDecimal v4 = TaxRateUtils.calculateTaxPayableByTaxBefore(num, new BigDecimal(0.17));
        System.out.println("根据税前价 " + num + " 计算 应缴税款:" + v4);
    }
}
